﻿Ext.define('app.ux.GSE.cdWeb',
{
    extend: 'Ext.window.Window',
    width: 850,
    height: 550,
    modal: true,
    closable: false,
    layout: 'fit',
    mol: '',
    img: '',
    mf: '',
    mw:'',
    initComponent: function () {
        var me = this;
        // me.addEvents('afterCommit');
        console.log('mol>>>>Init>>>');
        console.log(me.mol);
        me.items = [
            {
                xtype: 'tabpanel',
                items: [
                    this.GetWeb(),
                    this.GetGse(),
                    this.GetHD()
                ],
                listeners: {
                    beforetabchange: function (_me, newCard, oldCard, opt) {

                        if (!oldCard)
                        { return; }

                        console.log('我要关闭啦>>');
                        console.log(oldCard.title);
                        if (oldCard.title == '╬ Web') {
                            var ketcher = getKetcher();

                            if (ketcher) {
                                me.mol = ketcher.getMolfile();
                                console.log('update by ╬ Web>>mol');

                                me.GetPng();
                                // console.log(me.mol);
                            }

                        } else if (oldCard.title == '╬ HD') {
                            var jsDraw = getJsDraw();

                            if (jsDraw) {
                                me.mol = jsDraw.get("test").getMolfile();
                                console.log('update by ╬ HD>>mol');

                                me.GetPng();
                                // console.log(me.mol);
                            }

                        } else {
                            console.log('╬ GSE>>mol');
                        }
                        console.log('>>>mol');
                        console.log(me.mol);

                        console.log('我要开启啦>>');
                        console.log(newCard.title);
                        if (newCard.title == '╬ Web') {
                            var ketcher = getKetcher();

                            if (ketcher) {
                                console.log('load------------mol');
                                ketcher.setMolecule(me.mol);
                            } else {
                                console.log('load-----ketcher-------mol----Error');
                            }

                        } else if (newCard.title == '╬ HD') {
                            var jsDraw = getJsDraw();

                            if (jsDraw) {
                                jsDraw.get("test").setMolfile(me.mol);
                                // console.log(me.mol);
                            }

                        } else {
                            console.log('╬ GSE>>mol');
                        }


                    }
                }
            }
        ];
        me.buttons = [
            {
                text: '确定', scope: this, handler: function () {
                    me.GetMol();
                    if (me.img.length < 20 || parseFloat(me.mw) <= 1) {
                        me.GetPng(true);
                    } else {
                        me.fireEvent('afterCommit', me.mol, me.img,me.mf,me.mw);
                    }
                    console.log(me.mol);
                }
            },
            { text: '取消', handler: function () { me.close(); } }
        ] 
        me.callParent();
    },
    GetPng: function (isSubmit) {
        var me = this;
        if (me.mol && me.mol.length > 10) {
            Ext.Ajax.request({
                params:  JSON.stringify({
                    mol: me.mol
                }),
                url: '/Prm/Substance/PostPngByMol',
                timeout: 60000,
                success: function (resp, opts) {
                    var json = Ext.decode(resp.responseText);
                    console.log(json);
                    me.img = json.img;
                    me.mf = json.mf;
                    me.mw = json.mv;
                    me.showImg();
                    if (isSubmit) {
                        me.fireEvent('afterCommit', me.mol, me.img,me.mf,me.mw);
                    }

                },
                failure: function (resp, opts) {
                    var respText = Ext.util.JSON.decode(resp.responseText);
                    Ext.Msg.alert('错误', respText.error);
                }

            });
        }
        else {
            console.log("mol is empty");
            me.img = '';
        }
    },
    GetMol: function () {
        var me = this;
        var activeTab = me.query('tabpanel')[0].activeTab;
        if (activeTab.title == '╬ Web') {
            var ketcher = getKetcher();
            if (ketcher) {
                me.mol = ketcher.getMolfile();
                me.img = '';
                console.log('ketcher.getMolfile>>success');
                console.log(me.mol);
            }
            else {
                console.log('ketcher.getMolfile>>fail');
            }
        } else if (activeTab.title == '╬ HD') {
            var jsDraw = getJsDraw();
            if (jsDraw) {
                me.mol = jsDraw.get("test").getMolfile();
                me.img = '';
                console.log('jsDraw.getMolfile>>success');
                console.log(me.mol);
            }
            else {
                console.log('ketcher.getMolfile>>fail');
            }

        } else {
            console.log('╬ GSE>>mol');
        }
    },
    GetGse: function () {
        var me = this;
        if (!this.gse) {
            this.gse = new Ext.Panel(
                {
                    html: 'gse',
                    title: '╬ CDTool',
                    tbar: [
                        {
                            text: 'Paste',
                            handler: function (e) {
                                _____CallSocketCompentType = 'chemdraw';
                                _____CallSocketCompentId = me.getId();
                                _____ChemDrawSocket.send('GetImg');
                                _____ChemDrawSocket.send('GetCdx');
                                _____ChemDrawSocket.send('GetMol');
                            }
                        },
                {
                    text: 'Edit',
                    handler: function (e) {
                        var ______mol = '';
                        if (me.mol && me.mol.length > 10) {
                            ______mol = me.mol;
                        }
                        _____ChemDrawSocket.send('OpenMol:' + ______mol);
                    }
                }
                ,
                {
                    text: 'Clear',
                    handler: function (e) {
                        me.cdx = '';
                        me.img = '';
                        me.mol = '';
                        me.GetGse().update('___*___*___*___');
                    }
                }
                    ]
                });
        }
        return this.gse;
    },
    GetWeb: function () {
        var me = this;
        if (!this.cdweb) {
            this.cdweb = new Ext.Panel(
                {
                    title: '╬ Web',
                    html: '<iframe id="ketcherFrame" name="ketcherFrame" frameborder="0" style="width:777px; height: 460px;border-style: none"   src="/Content/ketcher/ketcher.html"></iframe>',
                    listeners: {
                        'afterrender': function () {
                            var ketcher = getKetcher();

                            if (ketcher) {
                                console.log('load------------mol');
                                ketcher.setMolecule(me.mol);
                            } else {
                                console.log('load-----ketcher-------mol----Error');
                            }
                        }
                    }
                });
        }
        return this.cdweb;
    },
    GetHD: function () {
        var me = this;
        if (!this.cdhd) {
            this.cdhd = new Ext.Panel(
                {
                    title: '╬ HD',
                    autoScroll: true,
                    html: '<iframe id="jsdrawFrame" name="jsdrawFrame" frameborder="0" style="width:777px; height: 530px;border-style: none"  src="/Content/jsdraw/jsdraw.html"   scrolling="no"></iframe>'
                });
        }
        return this.cdhd;
    },
    showImg: function () {
        var me = this;
        showImg(me.GetGse().getId(), me.img, 10, 'middle');
    },
    listeners: {
        afterrender: function () {
            var me = this;
            var mol = me.mol;
            var img = me.img;
            console.log('initial>>');
            var activeTab = me.query('tabpanel')[0];
            activeTab.setActiveTab(1);
            activeTab.setActiveTab(2);
            activeTab.setActiveTab(0);
            me.mol = mol;
            me.img = img;
            me.showImg();
        }
    }
})



